﻿namespace AlgorithmInterview.Chapter04._01;

// 找出数组中唯一的重复元素
public class Test01
{
    /// <summary>
    /// Hash 方法
    /// </summary>
    /// <param name="array"></param>
    /// <returns></returns>
    public int FindDup(int[] array)
    {
        if (array == null)
            return -1;
        int len = array.Length;
        Dictionary<int, int> hashTable = new Dictionary<int, int>();
        int i;
        for (i = 0; i < len - 1; i++)
        {
            hashTable.Add(i, 0);
        }

        for (i = 0; i < len; i++)
        {
            if (hashTable[array[i] - 1] == 0)
            {
                hashTable[array[i] - 1] = array[i] - 1;
            }
            else
            {
                return array[i];
            }
        }

        return -1;
    }
}